/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package lineage.packet.server;

import java.io.IOException;

import lineage.core.Opcodes;
import lineage.model.instance.L1PcInstance;
import lineage.packet.L1AbstractPacketServer;
import lineage.util.Util;

/**
 * 中毒状态(所有人可见的外观效果).
 * <p>
 * 自己的血条颜色也会相应变化
 * 
 * @version 2014年3月16日下午9:49:42
 * @author jrwz
 */
public final class S_PoisonStatus extends L1AbstractPacketServer {
    private byte[] _byte = null;
    /** 正常(没有中毒). */
    public static final int NORMAL = 0;
    /** 绿色(一般毒). */
    public static final int GREEN = 1;
    /** 灰色(木诅毒). */
    public static final int GRAY = 2;

    /**
     * 角色的中毒状态(全部可见的外观效果).
     * 
     * @param pc
     *            - 角色
     */
    public S_PoisonStatus(final L1PcInstance pc) {
        this(pc.getId(), pc.getPoisonType());
    }

    /**
     * 中毒状态(全部可见的外观效果).
     * 
     * @param objId
     *            - 唯一id
     * @param type
     *            - 类型：0.正常、1.绿色、2.灰色
     */
    public S_PoisonStatus(final int objId, final int type) {
        writeC(Opcodes.S_OPCODE_POISON);
        writeD(objId);

        int[] status = getStatus(type);
        writeC(status[0]);
        writeC(status[1]);
    }

    /**
     * 取得中毒状态.
     * 
     * @param type
     *            - 类型：0.正常、1.绿色、2.灰色
     * @return 中毒状态(int型数组)
     */
    private int[] getStatus(final int type) {
        int[] value = new int[2];
        switch (type) {
        case NORMAL: // 正常
            value[0] = 0x00;
            value[1] = 0x00;
            break;
        case GREEN: // 绿色
            value[0] = 0x01;
            value[1] = 0x00;
            break;
        case GRAY: // 灰色
            value[0] = 0x00;
            value[1] = 0x01;
            break;
        default:
            Util.message("错误信息：更改对象中毒状态时未知的值(" + type + ")\t正常范围应该为(" + NORMAL + "~" + GRAY + ")");
            break;
        }
        return value;
    }

    @Override
    public byte[] getContent() throws IOException {
        if (this._byte == null) {
            this._byte = this.getBytes();
        }
        return this._byte;
    }
}
